import sys
import os
import random
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtCore import *

class Demo(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.parent = parent
        mainLayout = QVBoxLayout()  

        toolBar = QToolBar()        
        btn = QPushButton('Add Top')
        btn.clicked.connect(self.OnAddTopClicked)
        toolBar.addWidget(btn)

        btn = QPushButton('Add Child')
        btn.clicked.connect(self.OnAddChildClicked)
        toolBar.addWidget(btn)        

        btn = QPushButton('Delete')
        btn.clicked.connect(self.OnDelClicked)
        toolBar.addWidget(btn)

        toolBar.addWidget(QLabel('CurrentItem:'))
        self.currentEdit = QLineEdit()
        toolBar.addWidget(self.currentEdit)
        btn = QPushButton('Update')
        btn.clicked.connect(self.OnUpdateClicked)
        toolBar.addWidget(btn) 

        self.treeWidget = QTreeWidget()
        self.treeWidget.setHeaderLabels(['ID','Value'])
        self.treeWidget.currentItemChanged.connect(self.OnCurrentItemChanged)
        mainLayout.addWidget(toolBar)
        mainLayout.addWidget(self.treeWidget)
        self.setLayout(mainLayout) 
    def OnCurrentItemChanged(self,current,pre):
        txt = f'ID:{current.text(0)},Value:{current.text(1)}'        
        self.currentEdit.setText(txt)
        self.parent.OnShowMessage(f"Current item '{txt}'")
    def OnAddTopClicked(self):
        id = random.randint(0, 999)
        arr = [f'{id}',f'Node{id}']
        self.treeWidget.addTopLevelItem(QTreeWidgetItem(arr))
        self.parent.OnShowMessage(f"Add top item '{arr[1]}'")
    def OnAddChildClicked(self):
        id = random.randint(0, 999)
        arr = [f'{id}',f'Node{id}']        
        current = self.treeWidget.currentItem()
        if current is None:
            self.treeWidget.addTopLevelItem(QTreeWidgetItem(arr))
        else:
            current.addChild(QTreeWidgetItem(current,arr))     
            current.setExpanded(True)   
        self.parent.OnShowMessage(f"Add item '{arr[1]}'")        
    
    def OnDelClicked(self):
        item = self.treeWidget.currentItem()
        if item.parent() is None:
            index=-1
            for i in range(self.treeWidget.topLevelItemCount()):
                if self.treeWidget.takeTopLevelItem(i).text(0)==item.text(0):
                    index=i
                    break
            self.treeWidget.removeItemWidget(item,index)
        else:
            for i in range(item.parent().childCount()):          
                child = item.parent().child(i) 
                if child.text(0)==item.text(0):                    
                    #item.parent().takeChild(i)
                    item.parent().removeChild(child)
                    del child
                    break
        self.parent.OnShowMessage(f"Delete item '{item.text(1)}'")
            
    def OnUpdateClicked(self):
        old = self.treeWidget.currentItem().text(1)
        new = self.currentEdit.text()
        self.treeWidget.currentItem().setText(1,new)
        self.parent.OnShowMessage(f"Update item from '{old}' to '{new}'")
    
def runDemo(parent):
    wigdet =  Demo(parent)
    return wigdet